/*
版本：v1.0
*说明：验证属性中优先验证非空约束当传入自定义正则时dataType失效，以当前传入的正则为准
*属性详解：
*tip:错误或提示信息，无需填写框架在验证之后给控件赋值，可自定义，在options给tip赋值既可,错误信息在config文件中配置
*label:标题，必选项 一般与左侧标题差不多
*dataType:所支持的属性参照帮助文档，去除了rangle属性 解决范围时无法限定数据类型的问题
*		  Group：需配合min和max两个属性否则无效（限单选和复选框使到） 需将同组的radio/checkbox放置在同个DIV中，
*min:整型或小数型，根据dataType不同而变化，当dataType为Group/Limit/LimitB时可选(默认值为0),
	当dataType不出现以上属性判断输入内容的长度(不区分中英文)。
*max:整型或小数型，根据dataType不同而变化，当dataType为Group/Limit/LimitB时可选(默认值为0),
	当dataType不出现以上属性判断输入内容的长度(不区分中英文)。
*operator:语法：operator="NotEqual | GreaterThan | GreaterThanEqual | LessThan | LessThanEqual | Equal"
          说明：字符串。在dataType属性值为Compare时可选(默认值为Equal)。
*require:语法：require="true | false"
		 说明：是否必填，控件为文本框时限定输入内容必需为空， 如果是checkbox和radio限定必需至少选择一项
to:字符串。当dataType值为Repeat或Compare时必选 对方控件的ID
*format：字符串 例：ymd|dmy|ymd h:m:s
*regexp:正则表达式 注：属性中的regexp不需要带开始和结束的/标记
*max和min的用法，在碰到数字类型的时候验证大小，碰到字符串类型的时候验证长度，除非指定dataType="Limit"否则默认验证时区分中英文 
*exclude表示排除验证 默认值为false
*precision：表示小数类型的小数位精度
*showTip:false表示追加提示文本默认值为true
*注：如果样式发生冲突，将引用CSS样式文件的顺序调整一下，将本验证框架用到的样式放在最后(原因暂时未清楚)
*	 文本框的样式为class=".a .b"; .a{backgroun-color:red}处于a.css文件中.b{background-color:black}处于b.css文件中，
*	 如果将b.css放在最后将应用黑色，如果将a.css放在最后将应用红色,其它不冲突的样式会取并集
*/
var EasyValidator = {
	initial:function(panel,mode,options){
		this.defaults = { 
			basePath:"",//工程的根目录
			xOffset:-20,// x distance from mouse
		    yOffset:20, // y distance from mouse
			label:"label",//以下代码为了防止冲突自定义属性
			tip:"tip",
			msg:"msg",
			showTip:"showTip",
			exclude:"exclude"
		};
		this.panel = panel;
		this.options = $.extend(this.defaults, options);	
		this.Regs = EasyValidator_defaultConfig.Regs;//正则表达式
		//this.effectiveTips = EasyValidator_defaultConfig.effectiveTips;//有效的
		this.InvalidTips = EasyValidator_defaultConfig.InvalidTips;//无效的
		this.vControls = [];
		this.invalidControls = [];
		this.attachValidateEvent();	
		this.createTipDiv();
	},
	attachValidateEvent:function(){
		var me = this;
		this.getValidateControls().each(function(){
			if(this.readOnly==true||this.readonly==="true"){//如果是只读的，不进行任何验证，也不加载提示
				return;
			}
			//tip属性加载
			me.initailTip(this);//在这里初始化
			$(this).bind("mouseover",function(e){//移上去的时候重新创建提示框，因为信息不一样
				me.panel.p_vtip.refreshTipDivText(this);
				//alert(me.options.showTip);
				var showTip = this.getAttribute(me.options.showTip);
				//alert(showTip);

				if(showTip==="false"){
					return;
				}
				$(me.panel.p_vtip).css("display","block");
				me.panel.p_vtip.setOffset({top:e.clientY,left:e.clientX});
			});

			$(this).bind("mouseout",function(e){//绑定公共事件主要用来显示tip
				
				var showTip = this.getAttribute(me.options.showTip);
				
				if(showTip==="false"){
					return;
				}
				$(me.panel.p_vtip).css("display","none");
			});
			$(this).bind("focus",function(e){
				var isOK = me.validate(this);
				if(isOK)
					me.change_error_style($(this),"remove");
				else
					me.change_error_style($(this),"add");
			});
			$(this).bind("blur",function(e){
				var isOK = me.validate(this);
				if(isOK)
					me.change_error_style($(this),"remove");
				else
					me.change_error_style($(this),"add");
			});
			$(this).bind("mousemove",function(e){
				var showTip = this.getAttribute(me.options.showTip);
				if(showTip==="false"){
					return;
				}
				me.panel.p_vtip.refreshTipDivText(this);
				me.panel.p_vtip.setOffset({top:e.clientY,left:e.clientX});
			});
			me.attachEventByCType(this);

		});		
	},
	attachEventByCType:function(obj){//根据控件类型绑定事件
			var me = this;
			if((obj.tagName=="INPUT"&&obj.type=="text")||obj.tagName=="TEXTAREA"||obj.type=="password"){//根据具体不同的控件绑定不同事件			
				me.attachTextChangeEvent(obj);
				var to = obj.getAttribute("to");
				if(to){
					var toSelector = "#"+to;
					me.attachTextChangeEventForTo(obj,$(toSelector).get(0));
				}
			}
			else if(obj.tagName=="DIV"){
				$(obj).bind("click",function(e){
					var isOK = me.validate(this);
					if(isOK)
						me.change_error_style($(this),"remove");
					else
						me.change_error_style($(this),"add");
				});
			}else if(obj.tagName=="SELECT"){
				$(obj).bind("click",function(e){
					var isOK = me.validate(this);
					if(isOK)
						me.change_error_style($(this),"remove");
					else
						me.change_error_style($(this),"add");
				});
			}
	},
	attachTextChangeEvent:function(obj){//绑定文本框的文本变更事件 是propertychange事件的优化
		var me = this;
		$(obj).bind("keydown",function(){
			this.setAttribute("oldValue",this.value);
		});
		$(obj).bind("keyup",function(){
			this.setAttribute("newValue",this.value);
			if(this.getAttribute("newValue") == this.getAttribute("oldValue")){
				return;
			}
			var isOK = me.validate(this);
			if(isOK)
				me.change_error_style($(this),"remove");
			else
				me.change_error_style($(this),"add");				
		});
	},
	attachTextChangeEventForTo:function(obj,toObj){//绑定文本框的文本变更事件 是propertychange事件的优化
		var me = this;
		$(toObj).bind("keydown",function(){
			this.setAttribute("oldValue",this.value);
		});
		$(toObj).bind("keyup",function(){
			this.setAttribute("newValue",this.value);
			if(this.getAttribute("newValue") == this.getAttribute("oldValue")){
				return;
			}
			var isOK = me.validate(obj);
			if(isOK)
				me.change_error_style($(obj),"remove");
			else
				me.change_error_style($(obj),"add");				
		});
	},
	//创建或替换提醒框
	createTipDiv:function(){
		var me = this;
		if(!this.EasyValidator_TempDiv){
			var EasyValidator_TempDiv = $('<div class="validate_arr" style="width:200px;height:200px;display:none;"/>');
			$('body').append(EasyValidator_TempDiv);
			this.EasyValidator_TempDiv = EasyValidator_TempDiv.get(0);			
		}
		if(this.panel.p_vtip){//如果已经创建就退出
			return;
		}
		var imgUrl = getCurrentStyle(this.EasyValidator_TempDiv).backgroundImage;
		function getCurrentStyle(_obj){//考虑样式的兼容性
			if(_obj.ownerDocument.defaultView){
				return _obj.ownerDocument.defaultView.getComputedStyle(_obj, null);
			}else if(_obj.currentStyle){
				return _obj.currentStyle;
			}

		}

		//imgUrl = imgUrl.substring(5,imgUrl.length-1);///在google浏览器中substring取值真不一样，郁闷
		imgUrl = imgUrl.replace("url(","");
		imgUrl = imgUrl.substring(0,imgUrl.length-1);
		imgUrl = imgUrl.replace(/\"/g,"");
		var p = $('<p id="vtip"></p>');
		var _tip = "";
		var img = $('<img id="vtipArrow" src="'+imgUrl+'"/>');
		var textNode = document.createTextNode(_tip);
		p.append(img);
		p.append($(textNode));

		p.get(0).setOffset = function(offset){
			var scrollTop = $(me.panel).scrollTop();
			var scrollLeft = $(me.panel).scrollLeft()||0;
			var top =  offset.top  + me.options.yOffset+scrollTop;
			var left = offset.left + me.options.xOffset+scrollLeft;


			/*var pWidth = $(me.panel).width();//容器的宽度
			var tWidth = $(this).width();//当前提示框的宽度
			if((left+tWidth)>pWidth){
				left = $(me).offset().left;
			}*/
			$(this).css("top", top+"px").css("left", left+"px")
		};
		p.get(0).refreshTipDivText = function(_obj){
			me.panel.tipTextNode.nodeValue =  _obj[me.options.tip]||"";
		};
		document.getElementsByTagName("body")[0].appendChild(p.get(0));
		this.panel.p_vtip = p.get(0);
		this.panel.tipTextNode = textNode;
		//$(this.panel).find("p#vtip").get(0).textNode = textNode;
	},
	//初始化每个控件的tip，如果没有加tip属性，将使用对应的默认属性
	initailTip:function(obj){
		//长度验证 最大长度验证 如果maxLength 如果没有max的时候获取maxlength属性
		/*var maxlength = obj.getAttribute("maxlength");
		if(maxlength&&!obj.getAttribute("max")){
			obj.setAttribute("max",maxlength);
		}*/

		//新加功能，如果页面中有添加自定义msg属性(默认为msg)就以该属性做为提示信息
		var msg = obj.getAttribute(this.options.msg);//新加功能
		if(msg!=""&&msg!=null){
			obj[this.options.tip] = msg;	
			return;
		}

		
		var _to = obj.getAttribute("to");
		var _dataType = obj.getAttribute("dataType");
		var _toSelector = "#"+_to;
		if(_to){
			_dataType = $(_toSelector).get(0).getAttribute("dataType");
		}
		/*if(_dataType=="Limit"||_dataType=="LimitB"){
			if(
		}*/

		var _tip = this.getTips(obj,_dataType);
		var _label = obj.getAttribute(this.options.label)||"";
		var _toLabel = "";
		if(_to){
			_toLabel = $(_toSelector).get(0).getAttribute(this.options.label)||"";
		}

		var _operator = obj.getAttribute("operator")||"";
		var _min = obj.getAttribute("min")||"";
		var _max = obj.getAttribute("max")||"";
		var _maxDataLenth = obj.getAttribute("maxDataLenth")||"";
		//alert("我啊我："+_tip);
		_tip = _tip.replace("@{label}",_label);
		_tip = _tip.replace("@{to}",_toLabel);
		_tip = _tip.replace("@{operator}",this.getCOperator(_operator));
		_tip = _tip.replace("@{min}",_min);
		_tip = _tip.replace("@{max}",_max);
		_tip = _tip.replace("@{length}",_maxDataLenth);//----

		obj[this.options.tip] = _tip;
	},
	//更改当前控件的tip属性
	change_tip:function(obj,msg){
		//新加功能，如果页面中有添加自定义msg属性(默认为msg)就以该属性做为提示信息
		var defaultMsg = obj.getAttribute(this.options.msg);//新加功能
		if(defaultMsg!=""&&defaultMsg!=null){
			obj[this.options.tip] = defaultMsg;	
			return;
		}
		obj[this.options.tip] = msg;
	},
	basicDataTypeChangeTip:function(obj,isValidate){//基础数据类型改变提示框
		var dataType = obj.getAttribute("dataType");
		if(obj.to){
			var toSelector = "#"+obj.to;
			dataType = $(toSelector).get(0).getAttribute("dataType");
		}
		obj.isValidate = isValidate;
		var _tip = this.getTips(obj,dataType);

		var _min = obj.getAttribute("min")||"";
		var _max = obj.getAttribute("max")||"";

		var _label = obj.getAttribute(this.options.label);
		var _maxDataLenth = obj.getAttribute("maxDataLenth")||"";

		_tip = _tip.replace("@{label}",_label);
		_tip = _tip.replace("@{min}",_min);
		_tip = _tip.replace("@{max}",_max);
		_tip = _tip.replace("@{length}",_maxDataLenth);

		this.change_tip(obj,_tip);
	},
	change_error_style:function(obj,action_type){//如果样式发生冲突，将引用CSS样式文件的顺序调整一下，将本样式放在最后
		if(action_type == "add"){
			obj.addClass("input_validation-failed");
		}else{
			obj.removeClass("input_validation-failed");
		}
	},
	IsSafe : function(str) {
		return !this.Regs.UnSafe.test(str);
	},
	limit : function(len, min, max) {
		min = min || 0;
		max = max || Number.MAX_VALUE;
		return min <= len && len <= max;
	},
	LenB : function(str) {
		return str.replace(/[^\x00-\xff]/g, "**").length;
	},
	ClearState : function(elem) {
		with (elem) {
		}
	},
	AddError:function(elem){
			
	},
	Exec : function(op, reg) {
		return new RegExp(reg, "g").test(op);
	},
	compare : function(obj1, operator, obj2) {
		var op1 = obj1.value;
		var op2 = obj2.value;

		var isNum_op1 = this.ValidateDataType(op1,"Number");
		var isNum_op2 = this.ValidateDataType(op2,"Number");
		
		var isDate_op1 = this.IsDate(op1);
		var isDate_op2 = this.IsDate(op2);
		if(!isNum_op1&&!isDate_op1){//只判断数字和日期
			return false;
		}
		
		if(isNum_op1&&isNum_op2){//数字比较大小
			//alert("数字比较大小");
			op1 = parseFloat(op1);
			op2 = parseFloat(op2);
			switch (operator) {
				case "NotEqual":
					return (op1 != op2);
				case "GreaterThan":
					return (op1 > op2);
				case "GreaterThanEqual":
					return (op1 >= op2);
				case "LessThan":
					return (op1 < op2);
				case "LessThanEqual":
					return (op1 <= op2);
				default:
					return (op1 == op2);
			}
		}else if(isDate_op1&&isDate_op2){//日期比较大小
			var _op1 = op1.replace(/-/g,"/");
			var _op2 = op2.replace(/-/g,"/");
			var _date1 = new Date(_op1);
			var _date2 = new Date(_op2);
			switch (operator) {
				case "NotEqual":
					return (_date1 != _date2);
				case "GreaterThan":
					return (_date1 > _date2);
				case "GreaterThanEqual":
					return (_date1 >= _date2);
				case "LessThan":
					return (_date1 < _date2);
				case "LessThanEqual":
					return (_date1 <= _date2);
				default:
					return (_date1 == _date2);
			}
		}
	},
	Range:function(obj,min,max){//判断是否在范围内
		var isNum_op1 = this.ValidateDataType(op1,"Number");
		var isNum_op2 = this.ValidateDataType(op2,"Number");
		if(!isNum_op1||!isNum_op2){//不是数字
			return false;
		}
	},
	MustChecked : function(obj, min, max){
		var checkedObjs = $(obj).find(":checkbox:checked");
		var hasChecked = 0;
		min = min || 1;
		max = max || groups.length;
		return min <= checkedObjs.length && checkedObjs.length<= max;
	},
	IsDate : function(op, formatString) {
		formatString = formatString || "ymd";
		var m, year, month, day;
		switch (formatString) {
		case "ymd":
			m = op.match(new RegExp(
					"^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
			if (m == null)
				return false;
			day = m[6];
			month = m[5]--;
			year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
			break;
		case "dmy":
			m = op.match(new RegExp(
					"^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
			if (m == null)
				return false;
			day = m[1];
			month = m[3]--;
			year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
			break;
		default:
			break;
		}
		if (!parseInt(month))
			return false;
		month = (month-1)%12;//月份从0开始到12
		var date = new Date(year, month, day);
		return (typeof (date) == "object" && year == date.getFullYear()
				&& month == date.getMonth() && day == date.getDate());
		function GetFullYear(y) {
			return ((y < 30 ? "20" : "19") + y) | 0;
		}
	},
	isNull:function(str){//自定义方法，校验空串
		if (null == str || ""== str.trim()) {   
			return true; 
		} else {   
			return false; 
		}
	},
	ValidateDataType:function(v,dataType){
		var regEx = this.Regs[dataType];
		if(!regEx){
			return true;
		}
		return regEx.test(v);
	},
	getCOperator:function(operator){//得到中文的操作符描述
		switch (operator) {
			case "NotEqual":
				return "不等于";
			case "GreaterThan":
				return "大于";
			case "GreaterThanEqual":
				return "大于等于";
			case "LessThan":
				return "小于";
			case "LessThanEqual":
				return "小于等于";
			default:
				return "等于";
		}
	},
	isEmpty:function(obj){//用于判断是个对像是否为空
		var radios =  $(obj).find(":radio:checked");
		var checkedObjs =   $(obj).find(":checkbox:checked");
		if(obj.tagName=="DIV"&&radios.length>0){//单选按钮
			return radios.length==0;
		}else if(obj.tagName=="DIV"&&checkedObjs.length>0){//单选按钮
			return checkedObjs.length==0;
		}else if((obj.tagName=="INPUT"&&obj.type=="text")||obj.tagName=="TEXTAREA"||obj.type=="password"||obj.type=="file"){//输入框
			var _isEmpty = false;
			var _v = obj.value;
			_v = _v.replace(/\s+/,"");
			return _v == "";
		}else if(obj.tagName=="SELECT"){
			return obj.value=="";
		}
		else{
			return true;
		}
	},
	undefinedOrEmpty:function(v){//判断一个值是否为undefined或空（null或“”）主要用于字符串的处理
		return v==undefined || v=="";
	},
	getTips:function(obj,dataType){
		//新加功能，如果页面中有添加自定方msg属性(默认为msg)就以该属性做为提示信息
		var msg = obj.getAttribute(this.options.msg);
		if(msg!=""&&msg!=null){
			return msg;
		}

		var _defaultTip = "defaultTip";
		if(obj.tagName=="SELECT"||obj.tagName=="DIV"){
			_defaultTip = "defaultTip_Choose";
		}

		var _tip = this.InvalidTips[dataType]||this.InvalidTips[_defaultTip]||"";
		
		var _min = obj.getAttribute("min");
		var _max = obj.getAttribute("max");
		if(typeof _tip == "object"){//说明
			if(!this.undefinedOrEmpty(_max) && this.undefinedOrEmpty(_min)){//是否只存在max
				return this.InvalidTips[dataType].lackMin;
			}else if (!this.undefinedOrEmpty(_min) && this.undefinedOrEmpty(_max)){//是否只存在min
				return  this.InvalidTips[dataType].lackMax;
			}else if(this.undefinedOrEmpty(_min) && this.undefinedOrEmpty(_max)){//是否min和max都不存在
				return this.InvalidTips["defaultTip"];
			}else{//都存在
				return this.InvalidTips[dataType].theFull;
			}
		}else{
			return _tip;
		}
	},
	minMaxValidate:function(obj){
		if(obj.tagName=="DIV"){
			obj.isValidate = true;
			return {success:true};
		}
		var _dataType = obj.getAttribute("dataType")||"Require";

		var _min = obj.getAttribute("min");
		var _max = obj.getAttribute("max");
		if(!_min&&!_max){//最小值和最大值取不到不验证
			return {success:true};
		}

		var strDataTypes = ["require","Chinese","English","Email","Url",
			"Phone","Mobile","Zip","IdCard","QQ","SafeString","LimitB","Limit","Chinese","English"];
		var NumberDataTypes = ["Number","UnsignedDouble","Integer","Double","Currency","UnsignedCurrency"];

		var strValidate = false;//字符串验证
		var numberValidate = false;//数字型验证
		for(var i=0;i<strDataTypes.length;i++){//遍历查找字符串数据类型
			if(_dataType==strDataTypes[i]){
				strValidate = true;
				break;
			}
		}
		for(var i=0;i<NumberDataTypes.length;i++){//遍历查找整型数据类型
			if(_dataType==NumberDataTypes[i]){
				numberValidate = true;
				break;
			}
		}

		var _v = obj.value;
		if(strValidate){//不在规定数据类型中不验证
			_min = _min||0;
			_max = _max||Number.MAX_VALUE;
			//var _success = _v.length>=_min&&_v.length<=_max;
			//alert(LenB(_v.length));
			var _success;
			var _errorType;
			if(_dataType&&(_dataType=="Chinese"||_dataType=="LimitB")){
				_success = this.LenB(_v)>=_min&&this.LenB(_v)<=_max;
				_errorType = "LimitB";
			}else{
				_success = _v.length>=_min&&_v.length<=_max;
				_errorType = "Limit";
			}			
			obj.isValidate = _success;
			return {success:_success,errorType:_errorType};
			//return _success;			
		}else if(numberValidate){
			_min = _min||0;
			_max = _max||Number.MAX_VALUE;
			
			//min = parseFloat(_min);
			//max = parseFloat(_max);
			_v = parseFloat(_v);
			var _success = false;
			_success =  _v>=_min && _v<=_max;
			obj.isValidate = _success;
			return {success:_success,errorType:"Range"};
		}else{
			obj.isValidate = true;
			return {success:true,errorType:"LimitB"};
		}

	},
	validate:function(obj){
		//alert(obj.dataType);
		//alert(this.isEmpty(obj));
		var me = this;
		//延迟修改tip的值 这样的写法，就不用在每个类型的返回上都写上这段代码
		setTimeout(function(){
			if(me.panel&&me.panel.p_vtip){
				me.panel.p_vtip.refreshTipDivText(obj);
			}
		},500);
		var _dataType = obj.getAttribute("dataType")||"";
		var _basicDataType = _dataType;
		var _require =  obj.getAttribute("require");
		var _to = obj.getAttribute("to")||"";
		if(_dataType=="require"){
			_require = "true";
		}
		var isValidate = false;
		if (_require==undefined||_require == "false"){//允许为空文本框没有任何值不进行任何验证
			if(this.isEmpty(obj)){
				this.initailTip(obj);//在这里初始化tip
				return true;
			}
		}

		if(_require == "true" && this.isEmpty(obj)){//不允许为空但是又为空时(违反非空验证的)
			var _label = obj.getAttribute(this.options.label)||"";
			var _tip = this.getTips(obj,"Require");
			_tip = _tip.replace("@{label}",_label);
			obj[this.options.tip] = _tip;
			obj.isValidate = false;
			this.change_tip(obj,_tip);
			return false;
		}
		

		if(obj.regexp){//有自定义正则表达式时
			//alert("自定义的");
			var regExStr = obj.getAttribute("regexp")||"";
			var regEx = new RegExp(regExStr);				
			isValidate = regEx.test(obj.value);

			var _tip = this.getTips(obj,"Custom")||"";
			var _label = obj.getAttribute(this.options.label);
			_tip = _tip.replace("@{label}",_label);
			obj[this.options.tip] = _tip;
			obj.isValidate = isValidate;
			this.change_tip(obj,_tip);

			return isValidate;
		}
		
		
		var validateResult = this.minMaxValidate(obj);//min以及max验证，包括选择项的多少和输入值的大小
		if(!validateResult.success){
			
			var _min = obj.getAttribute("min")||0;
			//var _max = obj.getAttribute("max")||Number.MAX_VALUE;
			var _max = obj.getAttribute("max");

			var _tip = this.getTips(obj,validateResult.errorType)||"";
			var _label = obj.getAttribute(this.options.label)||"";
			_tip = _tip.replace("@{label}",_label);
			_tip = _tip.replace("@{min}",_min);
			_tip = _tip.replace("@{max}",_max);
			this.change_tip(obj,_tip);
			return validateResult.success;
		}else{
			isValidate = true;
		}

		if(_to&&_to!=""){
			var toSelector = "#"+_to;
			_basicDataType = $(toSelector).get(0).getAttribute("dataType");
		}
		//这里只验证基本数据类型有to的验证当前对象数据类型需相同于to对应对象的数据类型，Repeat或Compare在switch后验证
		switch(_basicDataType)
		{
			case "Require":{//在前面验证过，是否不考虑Require属性//建议去掉
				break;
			}
			case "Email":{
				isValidate = this.ValidateDataType(obj.value,"Email");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Phone":{
				isValidate =  this.ValidateDataType(obj.value,"Phone");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Mobile":{
				isValidate = this.ValidateDataType(obj.value,"Mobile");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Url":{
				isValidate = this.ValidateDataType(obj.value,"Url");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "IdCard":{
				isValidate = this.ValidateDataType(obj.value,"IdCard");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Currency":{
				isValidate = this.ValidateDataType(obj.value,"Currency");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Number":{
				isValidate = this.ValidateDataType(obj.value,"Number");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Zip":{
				isValidate = this.ValidateDataType(obj.value,"Zip");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "QQ":{
				isValidate = this.ValidateDataType(obj.value,"QQ");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Integer":{
				isValidate = this.ValidateDataType(obj.value,"Integer");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Double":{
				isValidate = this.ValidateDataType(obj.value,"Double");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Chinese":{
				isValidate = this.ValidateDataType(obj.value,"Chinese");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "English":{
				isValidate = this.ValidateDataType(obj.value,"English");
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "SafeString":{
				isValidate = !this.ValidateDataType(obj.value,"UnSafe");					
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			case "Date":{
				isValidate = this.IsDate(obj.value);
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
			/********华丽的分隔符以上为基础数据类型验证，下面是一些特殊类型**********/
			case "Repeat":{//重复，常用到文本框	 在这里只验证基础数据类型				
				isValidate=true;break;
			}
			case "Limit":{//英文长度限制
				//alert("Limit");
				/*var min = obj.getAttribute("min")||0;
				var max = obj.getAttribute("max")||Number.MAX_VALUE;
				var v = obj.value;
				isValidate =  v.length >= min && v.length <= max;
				
				obj.isValidate = isValidate;
				var _tip = this.getTips(obj,"Limit");
				var _label = obj.getAttribute(this.options.label)||"";
				_tip = _tip.replace("@{label}",_label);
				_tip = _tip.replace("@{min}",min);
				_tip = _tip.replace("@{max}",max);
				this.change_tip(obj,_tip);*/
				break;
			}
			case "LimitB":{//中文长度限制
				/*var min = obj.getAttribute("min")||0;
				var max = obj.getAttribute("max")||Number.MAX_VALUE;
				var v = obj.value;
				isValidate =  this.LenB(v)>= min&& this.LenB(v) <= max;

				obj.isValidate = isValidate;
				var _tip =  this.getTips(obj,"Limit");
				var _label = obj.getAttribute(this.options.label)||"";
				_tip = _tip.replace("@{label}",_label);
				_tip = _tip.replace("@{min}",min);
				_tip = _tip.replace("@{max}",max);
				this.change_tip(obj,_tip);*/
				break;
			}
			case "Group":{//checkbox和radio专用				
				var min = obj.getAttribute("min")||0;
				var max = obj.getAttribute("max")||Number.MAX_VALUE;
				isValidate = this.MustChecked(obj,min,max);

				obj.isValidate = isValidate;
				var _tip = this.getTips(obj,"Group");
				var _label = obj.getAttribute(this.options.label)||"";
				_tip = _tip.replace("@{label}",_label);
				_tip = _tip.replace("@{min}",min);
				_tip = _tip.replace("@{max}",max);
				this.change_tip(obj,_tip);
				break;
			}
			case "Custom":{break;}
			case "":{isValidate=true;break;}
			default:{
				
				isValidate = this.ValidateDataType(obj.value,_basicDataType);
				this.basicDataTypeChangeTip(obj,isValidate);
				break;
			}
		}
		if(!isValidate){//如果数据类型都不满足就直接返回false
			//alert(isValidate);
			return isValidate;
		}
		//判断是否是小数类型，如果是小数类型验证小数类型的精度
		if(_basicDataType=="Currency"||_basicDataType=="UnsignedCurrency"
		||_basicDataType=="Double"||_basicDataType=="UnsignedDouble"){//判断是否有精度的属性
			var _precision = obj.getAttribute("precision")||"";
			if(_precision==""){//没有精度
			}
			var isNumber = this.ValidateDataType(_precision,"Number");
			if(!isNumber){//精度参数只能是整数
			}	
			if(_precision&&_precision!=""&&isNumber){
				var v = obj.value;
				var num_part = v.split(".");
				if(num_part.length==1){//没有小数位
					//alert("没有小数位");
				}else{//有小数位，得到小数位长度
					var _num2 = num_part[1];
					if(_num2.length > _precision){
						obj.isValidate = false;
						var _tip = this.getTips(obj,"precision");
						var _label = obj.getAttribute(this.options.label)||"";
						_tip = _tip.replace("@{label}",_label);
						_tip = _tip.replace("@{precision}",_precision);
						this.change_tip(obj,_tip);
						return success;						
					}
				}
			}
		}
		
		//取值
		var _min = obj.getAttribute("min");
		var _max = obj.getAttribute("max");
		//判断是否存在to属性，如果存在对两个控件进行比较
		if(_to&&_to!=""){//如果是比较时	
			var _toSelector = "#"+_to;
			var operator = obj.operator||"Equal";
			
			var success = false;
			success = this.compare(obj,operator,$(toSelector).get(0));


			obj.isValidate = success;
			var _tip = this.getTips(obj,"Compare");
			var c_operator = this.getCOperator(operator);
			var _label = obj.getAttribute(this.options.label)||"";
			var _tolabel = $(_toSelector).get(0).getAttribute(this.options.label)||"";
			_tip = _tip.replace("@{label}",_label);
			_tip = _tip.replace("@{to}",_tolabel);
			_tip = _tip.replace("@{operator}",c_operator);
			this.change_tip(obj,_tip);

			return success;
		}
		return isValidate;
			
	},
	pageValidate:function(panel){
		var me = this;
		var str= "";
		var invalidNum = 0;
		var invalidControls = [];
		var msgs = [];
		this.getValidateControls(panel).each(function(){
			if(this.readOnly==true){//如果是只读的，不进行任何验证，也不加载提示
				return;
			}
			var isOK = me.validate(this);
			if(isOK){
				me.change_error_style($(this),"remove");
			}
			else{
				me.change_error_style($(this),"add");
			}

			if(!isOK){
				invalidNum++;
				invalidControls.push(this);
				msgs.push(this[me.options.tip]);
				str+=invalidNum+"、"+this[me.options.tip]+"\r\n";
			}
		});
		var success = invalidControls.length==0?true:false;
		return {success:success,invalidControls:invalidControls,msg:str,msgs:msgs};
	},
	getValidateControls:function(_panel){//获取需验证的控件集合并返回
		var findPanel = _panel?_panel:this.panel;
		var controls = $(findPanel).find('[require],[dataType],[regexp],[min],[max],[maxlength],[to]');
		controls = controls.filter("[type!='submit']").filter("[type!='button']").filter("[type!='reset']");
		controls = controls.not(":disabled");//去除掉所有的disabled的控件
		//将排除的不加入到验证exclude
		var excludeAttrName = this.options["exclude"];
		var excludeSelector = "["+excludeAttrName+"='true']";
		controls = controls.not(excludeSelector);//去除掉所有的disabled的控件	
		return controls;
		
	}
};

//扩展String类功能//删除字符串二边空格    
String.prototype.trim = function()    {     
	return this.replace(/(^[\s]*)|([\s]*$)/g, "");    
}; 
//删除字符串左边空格    
String.prototype.lTrim = function()    {     
	return this.replace(/(^[\s]*)/g, "");    
};    
//删除字符串右边空格    
String.prototype.rTrim = function()    {     
	return this.replace(/([\s]*$)/g, "");    
};